package com.we823.project.controller.manage;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.project.controller.AbstractBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.we.ResourceModel;
import com.we823.project.repository.service.we.ResourceService;

@Controller
@RequestMapping(WebConstants.MANAGE_RESOURCE)
public class ResourceController extends AbstractBaseController<ResourceModel> {

	private String baseUrl = WebConstants.MANAGE_RESOURCE_TEMPLATE;
	
	private ResourceService resourceService;
	
	@Autowired
	public ResourceController(ResourceService resourceService) {
		super(resourceService);
		super.baseUrl = baseUrl;
		super.entityName="resource";
		this.resourceService=resourceService;
		setResourceIdentity("system:resource");
	}

	@Override
	public Page<ResourceModel> getData(PageRequest pageRequest,
			HttpServletRequest request, Model model, boolean pageable) {
		Page<ResourceModel> resourcePage = null;
		if(pageable) {
			resourcePage = resourceService.findAll(pageRequest);
		}else {
			resourcePage = new PageImpl<>(resourceService.findAll());
		}
		return resourcePage;
	}

	@Override
	public String preEdit(HttpServletRequest request, Model model) {
		List<ResourceModel> resourceModels = resourceService.findByParentid(0);
		model.addAttribute("parents",resourceModels);
		return super.preEdit(request, model);
	}
	
	@Override
	public String preAdd(HttpServletRequest request, Model model) {
		List<ResourceModel> resourceModels = resourceService.findByParentid(0);
		model.addAttribute("parents",resourceModels);
		return super.preAdd(request, model);
	}
}
